package com.example.black.myapplication.androidframwork;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2018/11/05/17:20
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class MyScrollView extends ScrollView {
    public static final String TAG = "MyScrollView";
    private ListView listView;
    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        String action = "no action";
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                action = "down";
                break;
            case MotionEvent.ACTION_MOVE:
                action = "move";
                break;
            case MotionEvent.ACTION_UP:
                action = "up";
                break;
        }
        Log.e(TAG, "MyScrollView: "+ event.getAction()+"desc "+action);
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (listView != null && checkArea(listView,ev)){
            return false;
        }
        return super.onInterceptTouchEvent(ev);
    }

    public boolean onTouchEvent(MotionEvent event){
        String action = "no action";
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                action = "down";
                break;
            case MotionEvent.ACTION_MOVE:
                action = "move";
                break;
            case MotionEvent.ACTION_UP:
                action = "up";
                break;
        }
        //requestDisallowInterceptTouchEvent(true);
        Log.e(TAG, "MyScrollView: "+ event.getAction()+"desc "+action);
        return super.onTouchEvent(event);
    }

    public void setListView(ListView listView) {
        this.listView = listView;
    }

    public boolean checkArea(View v,MotionEvent event){
        if (v == null){
            return false;
        }
        float x = event.getRawX();
        float y = event.getRawY();
        int[] locate = new int[2];
        v.getLocationOnScreen(locate);
        int l = locate[0];
        int r = l + v.getWidth();
        int t = locate[1];
        int b = t + v.getHeight();
        return l < x && x < r && t < y && y < b;
    }
}
